package com.baobao.dao;

import com.baobao.domain.Notice;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Notice entities.
 * 
 */
@Repository("NoticeDAO")
@Transactional
public class NoticeDAOImpl extends AbstractJpaDao<Notice> implements NoticeDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Notice.class }));

	/**
	 * EntityManager injected by Spring for persistence unit BaoBao
	 *
	 */
	@PersistenceContext(unitName = "BaoBao")
	private EntityManager entityManager;

	/**
	 * Instantiates a new NoticeDAOImpl
	 *
	 */
	public NoticeDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findNoticeBySchoolIdContaining
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeBySchoolIdContaining(String schoolId) throws DataAccessException {

		return findNoticeBySchoolIdContaining(schoolId, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeBySchoolIdContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeBySchoolIdContaining(String schoolId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeBySchoolIdContaining", startResult, maxRows, schoolId);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findNoticeByContentContaining
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeByContentContaining(String content) throws DataAccessException {

		return findNoticeByContentContaining(content, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeByContentContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeByContentContaining(String content, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeByContentContaining", startResult, maxRows, content);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findNoticeByContent
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeByContent(String content) throws DataAccessException {

		return findNoticeByContent(content, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeByContent
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeByContent(String content, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeByContent", startResult, maxRows, content);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findNoticeByPrimaryKey
	 *
	 */
	@Transactional
	public Notice findNoticeByPrimaryKey(String schoolId, java.util.Calendar time) throws DataAccessException {

		return findNoticeByPrimaryKey(schoolId, time, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeByPrimaryKey
	 *
	 */

	@Transactional
	public Notice findNoticeByPrimaryKey(String schoolId, java.util.Calendar time, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findNoticeByPrimaryKey", startResult, maxRows, schoolId, time);
			return (com.baobao.domain.Notice) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findNoticeBySummary
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeBySummary(String summary) throws DataAccessException {

		return findNoticeBySummary(summary, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeBySummary
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeBySummary(String summary, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeBySummary", startResult, maxRows, summary);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findNoticeByTitle
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeByTitle(String title) throws DataAccessException {

		return findNoticeByTitle(title, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeByTitle
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeByTitle(String title, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeByTitle", startResult, maxRows, title);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findNoticeBySchoolId
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeBySchoolId(String schoolId) throws DataAccessException {

		return findNoticeBySchoolId(schoolId, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeBySchoolId
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeBySchoolId(String schoolId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeBySchoolId", startResult, maxRows, schoolId);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findNoticeByTitleContaining
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeByTitleContaining(String title) throws DataAccessException {

		return findNoticeByTitleContaining(title, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeByTitleContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeByTitleContaining(String title, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeByTitleContaining", startResult, maxRows, title);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findNoticeByAuthorContaining
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeByAuthorContaining(String author) throws DataAccessException {

		return findNoticeByAuthorContaining(author, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeByAuthorContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeByAuthorContaining(String author, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeByAuthorContaining", startResult, maxRows, author);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findNoticeByTime
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeByTime(java.util.Calendar time) throws DataAccessException {

		return findNoticeByTime(time, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeByTime
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeByTime(java.util.Calendar time, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeByTime", startResult, maxRows, time);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findNoticeBySummaryContaining
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeBySummaryContaining(String summary) throws DataAccessException {

		return findNoticeBySummaryContaining(summary, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeBySummaryContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeBySummaryContaining(String summary, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeBySummaryContaining", startResult, maxRows, summary);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findNoticeByAuthor
	 *
	 */
	@Transactional
	public Set<Notice> findNoticeByAuthor(String author) throws DataAccessException {

		return findNoticeByAuthor(author, -1, -1);
	}

	/**
	 * JPQL Query - findNoticeByAuthor
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findNoticeByAuthor(String author, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findNoticeByAuthor", startResult, maxRows, author);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllNotices
	 *
	 */
	@Transactional
	public Set<Notice> findAllNotices() throws DataAccessException {

		return findAllNotices(-1, -1);
	}

	/**
	 * JPQL Query - findAllNotices
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Notice> findAllNotices(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllNotices", startResult, maxRows);
		return new LinkedHashSet<Notice>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Notice entity) {
		return true;
	}
}
